package router

import (
	"Free-Wind/conf"
	"Free-Wind/controller"
	"github.com/gin-gonic/gin"
	"github.com/natefinch/lumberjack"
	"os"
)

func Router() *gin.Engine {

	config := conf.LoadConfig()

	// 根据环境设置日志输出
	if config.Env == "dev" {
		// 开发环境：输出到控制台
		gin.DefaultWriter = os.Stdout
	} else {
		var logWriter = &lumberjack.Logger{
			Filename:   config.Log.Filename,
			MaxSize:    config.Log.MaxSize,    // MB
			MaxBackups: config.Log.MaxBackups, // 保留备份文件的最大数量
			MaxAge:     config.Log.MaxAge,     // 日志文件保存的最大天数
			Compress:   config.Log.Compress,   // 是否压缩旧的日志文件
		}
		gin.DefaultWriter = logWriter
	}
	r := gin.Default()
	// 认证模块
	auth := r.Group("/auth")
	authController := &controller.AuthController{}
	SetupAuthRoutes(auth, authController)
	// 系统模块
	system := r.Group("/system")
	systemController := &controller.SystemController{}
	SetupSystemRoutes(system, systemController)
	// 用户模块
	users := r.Group("/users")
	usersController := &controller.UsersController{}
	SetupUserRoutes(users, usersController)

	return r
}
